REDIM Statement ---------------------------------------------------------------------------- Action Changes the space allocated to an array that has been declared dynamic. Syntax REDIM SHARED variable( subscripts) AS type , variable( subscripts) AS type... Remarks The REDIM statement uses the following arguments. ----------------------------------------------------------------------------- Arguments Description ---------------------------------------------------------------------------- SHARED The optional SHARED attribute allows a module to share variables with all the procedures in the module; this differs from the SHARED statement, which affects only the variables within a single module. SHARED can be used in Arguments Description ---------------------------------------------------------------------------- module. SHARED can be used in REDIM statements only in the module-level code. variable A BASIC variable name. subscripts The dimensions of the array. Multiple dimensions can be declared. The subscript syntax is described below. AS type Declares the type of the variable. The type can be INTEGER, LONG, SINGLE, DOUBLE, STRING (for variable-length strings), STRING * length (for fixed-length strings), CURRENCY, or a user-defined type. Arguments Description ---------------------------------------------------------------------------- Subscripts in REDIM have the following form. lower TO upper , lower TO upper... The TO keyword provides a way to indicate both the lower and the upper bounds of an array's subscripts. The arguments lower and upper are numeric expressions that specify the lowest and highest value for the subscript. For more information about using the TO keyword, see the DIM statement. For more information about static and dynamic arrays, see Appendix B, "Data Types, Constants, Variables, and Arrays" in the Programmer's Guide. When a REDIM statement is compiled, all arrays declared in the statement are treated as dynamic. At run time, when a REDIM statement is executed, the array is deallocated (if it is already allocated) and then reallocated with the new dimensions. Old array-element values are lost because all numeric elements are reset to 0, and all string elements are reset to null strings. Although you can change the size of an array's dimensions with the REDIM statement, you can not change the number of dimensions. For example, the following statements are legal. ' $DYNAMIC DIM A(50,50) ERASE A REDIM A(20,15) ' Array A still has two dimensions. However, the following statements are not legal, and if you use them, BASIC generates the error message Wrong number of dimensions. ' $DYNAMIC DIM A(50,50) ERASE A REDIM A(5,5,5) ' Changed number of dimensions from two to three. Note BASIC now supports the CURRENCY data type (type suffix @). This is used in the AS type clause of REDIM. BASIC now supports static arrays in user-defined types. See Also DIM, ERASE Example The following example shows how to use REDIM to allocate an array of records and then how to free the memory that the records use. TYPE KeyElement Word AS STRING * 20 Count AS INTEGER END TYPE ' Make arrays dynamic. ' $DYNAMIC CLS ' Clear screen. ' Allocate an array of records when you need it. REDIM Keywords(100) AS KeyElement Keywords(99).Word = "ERASE" Keywords(99).Count = 2 PRINT "Keyword 99 is "; Keywords(99).Word PRINT "Count is"; Keywords(99).Count ' Free the space taken by Keywords when you're finished. ERASE Keywords END Output Keyword 99 is ERASE Count is 2